/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.lidovky.articles.articlesData.pesta;

/**
 *
 * @author x211800
 */
public class ColumnOfIssues extends Columns {

    private final String UNSPECIFIED = "neurčené";
    private boolean again = false;
    private int countOfIssues = 0;
    private int countOfDayTimes = 0;
    private StringBuffer issuesBuffer = new StringBuffer("");
    private StringBuffer dayTimes = new StringBuffer("");
    private String parsedIssues = null;
    private static ColumnOfIssues instance = null;
    private static final String STAR= "*";
    private StringBuffer note;

    private ColumnOfIssues(){
    }

    public static ColumnOfIssues instanceOf(String columnContent) throws ColumnsError{
        if (instance == null){
            instance = new ColumnOfIssues();
        }
        instance.parseColumn(columnContent);
        return instance;
    }

    /**
     *
     * @param column[6]
     * @return String{parsed issue numbers}
     * @throws ColumnsError{see message}
     */
    private void parseColumn(String column) throws ColumnsError {
        again = false;
        String issues = null;
        countOfIssues = 0;
        countOfDayTimes = 0;
        issuesBuffer = new StringBuffer("");
        dayTimes = new StringBuffer("");
        note = new StringBuffer("");

        /*
         * parsing issues and dayTime
         */
        if (!stringToInteger(column)) {
            issues = removeTextDelimits(column);
            if (!stringToInteger(issues)) {
                issues = dayTimeParser(issues);
            } else {
                countOfIssues = 1;
                addDayTime(UNSPECIFIED);
            }
        } else {

            issues = String.valueOf(column);
            countOfIssues = 1;
            addDayTime(UNSPECIFIED);
        }
        controlCounts();
        parsedIssues = issues;
    }

    private String dayTimeParser(String issues) throws ColumnsError {
        String[] splitedIssues = null;

        if (issues.contains(COMA)) {
            splitedIssues = removeWhiteSpaces(issues).split(COMA);

        } else {
            splitedIssues = issues.split(String.valueOf(WHITESPACE));
        }


        for (int i = 0; i < splitedIssues.length; i++) {
            again = false;
            secondPartDayTimeParser(splitedIssues[i]);
        }
        return issuesBuffer.toString();
    }

    private void addIssue(String part) {
        if (issuesBuffer.length() != 0) {
            issuesBuffer = issuesBuffer.append(COMA);
        }
        issuesBuffer = issuesBuffer.append(part);
        countOfIssues++;
    }

    private boolean isIssueWithSlash(String slashIssue) {
        String[] splitedIssue = removeWhiteSpaces(slashIssue).split(SLASH);
        return ((splitedIssue.length == 2) && stringToInteger(splitedIssue[0]) && stringToInteger(splitedIssue[1]));
    }

    private void secondPartDayTimeParser(String part) throws ColumnsError {

        if (!stringToInteger(part)) {
            if (isIssueWithSlash(part)) {
                String[] splitedIssue = removeWhiteSpaces(part).split(SLASH);
                addIssue(splitedIssue[1] + STAR);
                addDayTime(UNSPECIFIED);
            } else {
                analyzeDayTime(removeWhiteSpaces(part));
            }
        } else {
            addIssue(part);
            addDayTime(UNSPECIFIED);
        }
    }

    private void addDayTime(String dayTime) {
        if (dayTimes.length() != 0) {
            dayTimes.append(COMA);
        }
        dayTimes.append(dayTime);
        countOfDayTimes++;
    }

    private void analyzeDayTime(String unparsed) throws ColumnsError {
        String toAnalyze = removeDiacritics(unparsed.toLowerCase());

        if (toAnalyze.equals("v.") || toAnalyze.equals("v")) {
            addDayTime("večerní");

        } else if (toAnalyze.equals("r.") || toAnalyze.equals("r")) {
            addDayTime("ranní");

        } else if (toAnalyze.equals("malevyd.")
                || toAnalyze.equals("malevyd")
                || toAnalyze.equals("malevydani")
                || toAnalyze.equals("malevyd.,pril.")
                || toAnalyze.equals("malavyd")
                || toAnalyze.equals("malavyd.")
                || toAnalyze.equals("malavydani")) {
            addDayTime("malé");

        }else if (toAnalyze.equals("pril")
                || toAnalyze.equals("pril.")
                || toAnalyze.equals("priloha")){
                if (note.length() != 0) {
                    note.append(COMA + WHITESPACE);
                }
                note.append("příloha");

        }else if (toAnalyze.equals(UNSPECIFIED)){
            addDayTime(UNSPECIFIED);
        }else if (toAnalyze.equals("malevyd.pril.")){
            addDayTime("malé");
            if (note.length() != 0) {
                    note.append(COMA + WHITESPACE);
                }
                note.append("příloha");
        }else if (!again && toAnalyze.contains("male")) {

            again = true;
            secondPartDayTimeParser(toAnalyze.substring(0, toAnalyze.indexOf("male")));
            secondPartDayTimeParser(toAnalyze.substring(toAnalyze.indexOf("male"), toAnalyze.length()));

        } else if (!again && toAnalyze.contains("v")) {
            again = true;
            secondPartDayTimeParser(toAnalyze.substring(0, toAnalyze.indexOf("v")));
            secondPartDayTimeParser(toAnalyze.substring(toAnalyze.indexOf("v"), toAnalyze.length()));


        } else if (!again && toAnalyze.contains("r")) {
            again = true;
            secondPartDayTimeParser(toAnalyze.substring(0, toAnalyze.indexOf("r")));
            secondPartDayTimeParser(toAnalyze.substring(toAnalyze.indexOf("r"), toAnalyze.length()));

        } else if (!again && toAnalyze.contains("pril")) {
            again = true;
            secondPartDayTimeParser(toAnalyze.substring(0, toAnalyze.indexOf("pril")));
            secondPartDayTimeParser(toAnalyze.substring(toAnalyze.indexOf("pril"), toAnalyze.length()));
            
        }else{
            throw new ColumnsError("problem with converting day time");
        }
    }

    private void controlCounts() {
        if (countOfDayTimes == 0 && countOfIssues != 0) {
            dayTimes.append(UNSPECIFIED);
            for (int i = 0; i < countOfIssues - 1; i++) {
                dayTimes.append(COMA + UNSPECIFIED);
            }
            countOfDayTimes = countOfIssues;
        }
    }

    /**
     * 
     * @return int{count of times}
     */
    public int getCountOfDayTimes() {
//        System.out.println(countOfDayTimes);
        return countOfDayTimes;
    }

    /**
     *
     * @return int{count of issues}
     */
    public int getCountOfIssues() {
        return countOfIssues;
    }

    /**
     *
     * @return String{parsed day times}
     */
    public String getDayTimes() {
        return dayTimes.toString();
    }

    public String getParsedIssues() {
        return parsedIssues;
    }

    public StringBuffer getNote() {
        return note;
    }

    
}
